﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CSharp_Sample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(s =>
            {
                var p = new Progress(Int32.Parse(s.ToString()));
                p.ProgressUpdated += P_ProgressUpdated;
                p.Start();
            }, progressBar1.Maximum);
        }

        private void P_ProgressUpdated(object sender, ProgressEventArgs e)
        {
            if (InvokeRequired)
            {
                this.Invoke(new Action<object, ProgressEventArgs>(P_ProgressUpdated), sender, e);
                return;
            }
            progressBar1.Value = e.Percentage;
            if (e.Done)
            {
                progressBar1.Text = "Completed!";
            }
        }
    }
}
